import { Interface } from 'yapi-to-typescript';
import { genApiInterfaceName } from './interface';
import { upperFirst } from 'lodash';

/**
 * @description 下划线转驼峰
 * @author Wynne
 * @date 2021-06-25
 * @export
 * @param name
 * @param [isBigHump=false] 是否大驼峰
 * @return {*}
 */
export function underlineToHump(name: string, isBigHump = false): string {
  let hump = name.replace(/[_|\-|\s](\w)/g, function (all, letter) {
    return letter.toUpperCase();
  });
  hump =
    (isBigHump ? hump.charAt(0).toLocaleUpperCase() : hump.charAt(0).toLocaleLowerCase()) +
    hump.substr(1);
  return hump;
}

/**
 * @description 接口path路径变为驼峰
 * @author Wynne
 * @date 2021-07-01
 * @export
 * @param api
 * @param [isBigHump=true]
 * @return {*}
 */
export function pathToHump(api: Interface, isBigHump = true): string {
  let name = api.path.replace(/[{|}]/g, '').replace(/\//g, '_');
  name = underlineToHump(name, isBigHump);
  return name;
}
export const getRequestName = (interfaceInfo: Interface): string => {
  const method = interfaceInfo.method.toLowerCase();
  let query = 'Request';
  if (interfaceInfo.req_query.length > 0) {
    query = 'Query';
  }
  if (interfaceInfo.req_params.length > 0) {
    query = 'Raw';
  }
  if (interfaceInfo.req_body_form && interfaceInfo.req_body_form.length > 0) {
    query = 'Form';
  }
  return 'I' + upperFirst(method) + genApiInterfaceName(interfaceInfo, true, query);
};

export const getResponseName = (interfaceInfo: Interface) => {
  const method = interfaceInfo.method.toLowerCase();
  let query = '';
  if (interfaceInfo.req_params.length > 0) {
    query = 'Raw';
  }
  return 'I' + upperFirst(method) + query + genApiInterfaceName(interfaceInfo, true, 'Response');
};
